package com.zyd.controller;

import com.zyd.constant.MessageConstant;
import com.zyd.controller.result.PageResult;
import com.zyd.controller.result.QueryPageBean;
import com.zyd.controller.result.Result;
import com.zyd.dto.SetmealDto;
import com.zyd.entity.Setmeal;
import com.zyd.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "套餐管理")
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    // 分页查询
    @ApiOperation("分页查询")
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        System.out.println("接收到的分页参数: " + queryPageBean);
        PageResult pageResult = setmealService.findPage(queryPageBean);
        System.out.println("返回的分页结果 - 总数: " + pageResult.getTotal() + ", 记录数: " + pageResult.getRows().size());
        return Result.success(MessageConstant.QUERY_CHECKITEM_SUCCESS, pageResult);
    }

    @ApiOperation("更新套餐及检查组")
    @PutMapping("/updateWithGroups")
    public Result updateWithGroups(@RequestBody SetmealDto dto) {
        boolean success = setmealService.updateWithGroups(
                dto.getSetmeal(),
                dto.getCheckGroupIds()
        );
        return success ? Result.success("更新成功") : Result.error("更新失败");
    }

    @ApiOperation("删除套餐及关联关系")
    @DeleteMapping("/deleteWithGroups/{id}")
    public Result deleteWithGroups(@PathVariable Integer id) {
        boolean success = setmealService.deleteWithGroups(id);
        return success ? Result.success("删除成功") : Result.error("删除失败");
    }


    //
    @ApiOperation("addWithGroups")
    @PostMapping("/add")
    public Result add(@RequestBody SetmealDto setmealDto) {
        setmealService.addWithGroups(setmealDto.getSetmeal(), setmealDto.getCheckGroupIds());
        return Result.success(MessageConstant.ADD_SETMEAL_SUCCESS);
    }


}